<?php
/**
 * 产品类
 * @author chenlisong E-mail:chenlisong1021@163.com 
 * @version 创建时间：2016年10月24日 上午11:58:32 
 * 类说明
 */
class ProductAction extends BaseAction{
	
	public function index(){
		echo $this->err("非法操作！");
	}
	
	/**
	 * 获取所有商品
	 */
	public function getAllProduct(){
		$Product = M('Product');$Subtype = M('Subtype');$Type = M('Type');$ShopAttributeValues = M("ShopAttributeValues");
		$param = $this->param;
		$where['flag'] = 1;
		$where['is_on'] = 1;
		if(S('S_PRODUCT')){//启用缓存
			$tData = S('S_PRODUCT');
		}else{
			//检索大类
			$tData['typeList'] = $Type->where('reserve = 1 and flag = 1')->field('id,type_name as typeName')->order('seq asc')->select();
			$field = 'id,type_id as typeId,subtype_code as subTypeCode,name,photo,spec,price,sell_num as sellNum,seq,is_attr,tag_type';
			foreach ($tData['typeList'] as $tk=>$tv){
				$sData = $Subtype->where('flag = 1 and parent_id = '.$tv['id'])->field('parent_id as typeId,subtype_code as subTypeCode,subtype_name as subTypeName')->order('seq asc')->select();
				$tData['typeList'][$tk]['subTypeList'] = $sData;
			}
			$productData = $Product->where($where)->order('seq asc')->field($field)->select();
			foreach($productData as $p_key=>$p_v){
				if($p_v['is_attr']==1){
					$attr_arr['product_id'] = $p_v['id'];
					$attr_arr['flag'] = 1;
					if(isset($param['shop_id'])){
						$attr_arr['shop_id'] = $param['shop_id'];
					}
					$attribute_values = $ShopAttributeValues->field("id,attr_id,attr_name,attr_value_name,is_default,num")->where($attr_arr)->order('seq desc')->select();
					if(!empty($attribute_values)){
						$productData[$p_key]['attribute_values'] = $attribute_values;
						$attr_num = 0;
						foreach($attribute_values as $a_v_key=>$a_v_v){
							$attr_num = $attr_num+$a_v_v['num'];
						}
						$productData[$p_key]['num']=$attr_num;
					}
				}
				if($p_v['tag_type']){
					$productData[$p_key]['tag_ico'] = "/Uploads/type_ico/tag_".$p_v['tag_type'].".jpg";
				}
			}
			$tData['productList'] = $productData;
			S('S_PRODUCT',$tData,86400);//每天更新一次缓存，销量数据一天一更新
		}
		echo $this->suc($tData, 0);
	}
	
	/**
	 * 获取商品分类
	 */
	public function getTypeList(){

		$Subtype = M('Subtype');$Type = M('Type');$ShopType = M('ShopType');$ShopSubtype = M('ShopSubtype');
		$param = $this->param;
		$type_where['a.reserve'] = 1;
		$type_where['a.flag'] = 1;
		$type_where['b.shop_id'] = $param['shop_id'];
		$type_where['b.flag'] = 1;
		//检索大类
		$data['typeList'] = $ShopType->alias('b')->join('left join __TYPE__ as a on a.id = b.type_id')->where($type_where)->field('a.id,a.type_name as typeName')->order('a.seq asc')->select();
		foreach ($data['typeList'] as $tk=>$tv){
			$s_where['a.flag'] = 1;
			$s_where['a.parent_id'] = $tv['id'];
			$s_where['b.shop_id'] = $param['shop_id'];
			$s_where['b.flag'] = 1;
			$sData = $ShopSubtype->alias('b')->join('left join __SUBTYPE__ as a on a.subtype_code = b.subtype_code')->where($s_where)->field('a.parent_id as typeId,a.subtype_code as subTypeCode,a.subtype_name as subTypeName')->order('a.seq asc')->select();
			$data['typeList'][$tk]['subTypeList'] = $sData;
		}
		echo $this->suc($data, 0);
	}
	
	/**
	 * 根据分类Id获取商品
	 */
	public function getProductByTypeId(){
		$ShopProduct = M('ShopProduct');$ShopAttributeValues = M("ShopAttributeValues");
		$param = $this->param;
		if(is_null($param['nextStartId'])){
			echo $this->err('参数错误！');
			return ;
		}
		$where['a.flag'] = 1;
		$where['a.is_on'] = 1;
		if($param['typeId']!=0 && $param['typeId']!=""){
			$where['a.type_id'] = $param['typeId'];
		}
		if(isset($param['shop_id'])){
			$where['b.shop_id'] = $param['shop_id'];
		}else{
			$where['b.shop_id'] = 0;
		}
		$where['b.flag'] = 1;
		if(!empty($param['subTypeCode'])){
			$where['a.subtype_code'] = $param['subTypeCode'];
		}

		//自定义排序编码
		switch ($param['sort']){
			case '101': $sort = 'a.seq asc'; break;//seq asc
			case '102': $sort = 'a.sell_num desc'; break;//sell_num desc
			case '103': $sort = 'a.price asc'; break;//price asc
			case '104': $sort = 'a.price desc'; break;//price desc
			default:$sort = 'a.seq asc';
		}
		$count = $ShopProduct->alias('b')->join('left join __PRODUCT__ as a on a.id = b.product_id')->where($where)->count();
		//$field = 'a.id,a.type_id as typeId,a.subtype_code as subTypeCode,a.name,a.photo,a.spec,if(b.shop_price>0,b.shop_price,a.price) as price,a.sell_num as sellNum,a.seq,b.num as num,a.is_attr as is_attr,a.tag_type as tag_type';
		$field = 'a.id,a.type_id as typeId,a.subtype_code as subTypeCode,a.name,a.photo,a.spec,CASE WHEN  b.shop_price>0 THEN  b.shop_price ELSE a.price END AS price,a.sell_num as sellNum,a.seq,b.num as num,a.is_attr as is_attr,a.tag_type as tag_type';
		if($count < 12){
			$nextStartId = -1;
			$data = $ShopProduct->alias('b')->join('left join __PRODUCT__ as a on a.id = b.product_id')->where($where)->field($field)->order($sort)->select();
		}else{
			$nextStartId = $param['nextStartId'];
			if($param['nextStartId']!=-1){
				$data = $ShopProduct->alias('b')->join('left join __PRODUCT__ as a on a.id = b.product_id')->where($where)->field($field)->limit($param['nextStartId'],12)->order($sort)->select();
				$nextStartId = $nextStartId + 12;
			}else{
				$data = $ShopProduct->alias('b')->join('left join __PRODUCT__ as a on a.id = b.product_id')->where($where)->field($field)->limit(0,12)->order($sort)->select();
				$nextStartId = 12;
			}
			if($nextStartId >= $count || count($data) == 0){
				$nextStartId = -1;
			}
		}
		foreach($data as $p_key=>$p_v){
			if($p_v['is_attr']==1){
				$attr_where['product_id'] = $p_v['id'];
				$attr_where['flag'] = 1;
				if(isset($param['shop_id'])){
					$attr_where['shop_id'] = $param['shop_id'];
				}
				$attribute_values = $ShopAttributeValues->field("id,attr_id,attr_name,attr_value_name,is_default,num")->where($attr_where)->order('seq desc')->select();
				if(!empty($attribute_values)){
					$data[$p_key]['attribute_values'] = $attribute_values;
					$attr_num = 0;
					foreach($attribute_values as $a_v_key=>$a_v_v){
						$attr_num = $attr_num+$a_v_v['num'];
					}
					$data[$p_key]['num']=$attr_num;
				}
			}
			if($p_v['tag_type']){
				$data[$p_key]['tag_ico'] = "/Uploads/type_ico/tag_".$p_v['tag_type'].".jpg";
			}
		}
		echo $this->suc($data,$nextStartId);
	}
	
	/**
	 * 获取版本号
	 */
	public function getProductVersion(){
		if(!S('S_PRODUCT_VERSION')){
			S('S_PRODUCT_VERSION',10000);
		}
		$data['version'] = S('S_PRODUCT_VERSION');
		echo $this->suc($data);
	}
	
	/**
	 * 获取商品详情
	 */
	public function getProductInfo(){
		$Product = M('Product'); $ProductCollection = M('ProductCollection');$ShopProduct = M("ShopProduct");$ShopAttributeValues = M("ShopAttributeValues");
		$param = $this->param;
		$where['flag'] = 1;

		$where['id'] = $param['productId'];
		$field = 'id,name,num,photo,desc,spec,price,content,subtype_code,tag_type';
		$data = $Product->where($where)->field($field)->find();
		if(isset($param['shop_id'])){
			$ShopProductInfo = $ShopProduct->where(array('shop_id'=>$param['shop_id'],"product_id"=>$param['productId'],'flag'=>1))->find();
			$data['num'] = $ShopProductInfo['num'];
			if($ShopProductInfo['shop_price']>0){
				$data['price'] = $ShopProductInfo['shop_price'];
			}
		}else{
			$data['num'] = 0;
		}
		$attribute_values = $ShopAttributeValues->field("id,attr_id,attr_name,attr_value_name,is_default,num")->where(array("product_id"=>$param['productId'],"flag"=>1,'shop_id'=>$param['shop_id']))->order('seq desc')->select();
		if($attribute_values){
			$p_num = 0;
			foreach($attribute_values as $a_v){
				$p_num = $p_num+$a_v['num'];
			}
			$data['num'] = $p_num;
			$data['attribute_values'] = $attribute_values;
		}
		if(empty($data)){
			echo $this->err("未找到该商品！");
			return;
		}
		$data['is_collection'] = false;
		//获取用户是否收藏这款商品
		if(!empty($param['userId'])){
			$map['user_id'] = $param['userId'];
			$map['product_id'] = $param['productId'];
			$num = $ProductCollection->where($map)->count();
			if($num != 0){
				$data['is_collection'] = true;
			}
		}
		//获取推荐商品
		$list_where['a.flag'] = 1;
		$list_where['a.is_on'] = 1;
		$list_where['a.subtype_code'] = $data['subtype_code'];
		if(isset($param['shop_id'])){
			$list_where['b.shop_id'] = $param['shop_id'];
		}else{
			$list_where['b.shop_id'] = 0;
		}
		$list_where['b.flag'] = 1;
		//$list_field = 'a.id as id,a.name as name,a.photo as photo,a.price as price,a.spec as spec,b.num as num,a.is_attr,a.tag_type';
		$list_field = 'a.id as id,a.name as name,a.photo as photo,CASE WHEN  b.shop_price>0 THEN  b.shop_price ELSE a.price END AS price,a.spec as spec,b.num as num,a.is_attr,a.tag_type';
		$productList = $ShopProduct->alias('b')->join('left join __PRODUCT__ as a on a.id = b.product_id')->where($list_where)->limit(3)->order("rand()")->field($list_field)->select();
		if(empty($productList)){
			$data['productList'] = array();
		}else{
			$data['productList'] = $productList;
			foreach($data['productList'] as $p_key=>$p_v){
				if($p_v['is_attr']==1){
					$attribute_values = $ShopAttributeValues->field("id,attr_id,attr_name,attr_value_name,is_default,num")->where(array("product_id"=>$p_v['id'],"flag"=>1,'shop_id'=>$param['shop_id']))->order('seq desc')->select();
					if(!empty($attribute_values)){
						$data['productList'][$p_key]['attribute_values'] = $attribute_values;
						$attr_num = 0;
						foreach($attribute_values as $a_v_key=>$a_v_v){
							$attr_num = $attr_num+$a_v_v['num'];
						}
						$data['productList'][$p_key]['num']=$attr_num;
					}
				}
				if($p_v['tag_type']){
					$data['productList'][$p_key]['tag_ico'] = "/Uploads/type_ico/tag_".$p_v['tag_type'].".jpg";
				}
			}
		}

		//$data['productList'] = $Product->where('flag = 1 and is_on = 1 and subtype_code = '.$data['subtype_code'])->limit(3)->order("rand()")->field('id,name,photo,price,spec')->select();
		echo $this->suc($data);
	}
	
	/**
	 * 搜索数据准备
	 */
	public function goSearchProduct(){
		$HotKeyword = M('HotKeyword');
		$param = $this->param;
		$data = $HotKeyword->where('flag = 1')->order('num desc')->limit(10)->field('keyword')->select();
		echo $this->suc($data);
	}
	
	/**
	 * 搜索产品
	 */
	public function searchProduct(){
		$ShopProduct = M('ShopProduct');$ShopAttributeValues = M("ShopAttributeValues");
		//$Product = M('Product');
		$param = $this->param;
		if(is_null($param['nextStartId'])){
			echo $this->err('参数错误！');
			return ;
		}
		if(empty($param['keyword'])){
			echo $this->err("请输入关键字！");
			return;
		}
		$where['a.flag'] = 1;
		$where['a.is_on'] = 1;
		$where['b.shop_id'] = $param['shop_id'];
		$where['a.name|desc'] = array('like','%'.$param['keyword'].'%');
		$where['b.flag'] = 1;
		//$field = 'a.id,a.name,a.photo,a.price,a.spec,a.is_attr,a.tag_type,b.num';
		$field = 'a.id,a.name,a.photo,CASE WHEN  b.shop_price>0 THEN  b.shop_price ELSE a.price END AS price,a.spec,a.is_attr,a.tag_type,b.num';
		$count = $ShopProduct->alias('b')->join('left join __PRODUCT__ as a on a.id = b.product_id')->where($where)->count();
		if($count < 10){
			$nextStartId = -1;
			$data = $ShopProduct->alias('b')->join('left join __PRODUCT__ as a on a.id = b.product_id')->where($where)->field($field)->order('a.seq desc')->select();
		}else{
			$nextStartId = $param['nextStartId'];
			$data = $ShopProduct->alias('b')->join('left join __PRODUCT__ as a on a.id = b.product_id')->where($where)->field($field)->limit($nextStartId,10)->order('a.seq desc')->select();
			$nextStartId = $nextStartId + 10;
			if($nextStartId >= $count || count($data) == 0){
				$nextStartId = -1;
			}
		}
		foreach($data as $p_key=>$p_v){
			if($p_v['is_attr']==1){
				$attribute_values = $ShopAttributeValues->field("id,attr_id,attr_name,attr_value_name,is_default,num")->where(array("product_id"=>$p_v['id'],"flag"=>1,'shop_id'=>$param['shop_id']))->order('seq desc')->select();
				if(!empty($attribute_values)){
					$data[$p_key]['attribute_values'] = $attribute_values;
					$attr_num = 0;
					foreach($attribute_values as $a_v_key=>$a_v_v){
						$attr_num = $attr_num+$a_v_v['num'];
					}
					$data[$p_key]['num']=$attr_num;
				}
			}
			if($p_v['tag_type']){
				$data[$p_key]['tag_ico'] = "/Uploads/type_ico/tag_".$p_v['tag_type'].".jpg";
			}
		}
		echo $this->suc($data,$nextStartId);
	}
	
	/**
	 * 获取会场内容
	 */
	public function getMeetingProductInfo(){
		$Meetingplace = M('Meetingplace');$Product = M('Product');$MeetingplaceProduct = M('MeetingplaceProduct');$ShopAttributeValues = M("ShopAttributeValues");$ShopProduct = M("ShopProduct");
		$param = $this->param;
		$data = $Meetingplace->where('flag = 1 and is_on = 1 and id = '.$param['meetingId'])->field('id,title,img_app as img,desc')->find();
		if(empty($data)){
			echo $this->err("会场不存在！");
			return ;
		}
		$productIds = $MeetingplaceProduct->where('meetingplace_id = '.$param['meetingId'].' AND shop_id='.$param['shop_id'])->getField('GROUP_CONCAT(product_id)');
		$where['b.flag'] = 1;
		$where['b.is_on'] = 1;
		$where['a.product_id'] = array('in',$productIds);
		$where['a.shop_id'] = $param['shop_id'];
		$where['a.flag'] = 1;
		if($productIds){
			//$field = 'b.id,b.name,b.photo,b.price,b.spec,b.is_attr,b.tag_type,a.num';
			$field = 'b.id,b.name,b.photo,CASE WHEN  a.shop_price>0 THEN a.shop_price ELSE b.price END AS price,b.spec,b.is_attr,b.tag_type,a.num';
			$data['productList'] = $ShopProduct->alias('a')->join('left join __PRODUCT__ as b on b.id = a.product_id')->where($where)->order('FIELD(a.product_id,'.$productIds.')')->field($field)->select();
			foreach($data['productList'] as $p_key=>$p_v){
				if($p_v['is_attr']==1){
					$attr_where['product_id'] = $p_v['id'];
					$attr_where['flag'] = 1;
					if(isset($param['shop_id'])){
						$attr_where['shop_id'] = $param['shop_id'];
					}
					$attribute_values = $ShopAttributeValues->field("id,attr_id,attr_name,attr_value_name,is_default,num")->where($attr_where)->order('seq desc')->select();
					if(!empty($attribute_values)){
						$data['productList'][$p_key]['attribute_values'] = $attribute_values;
						$attr_num = 0;
						foreach($attribute_values as $a_v_key=>$a_v_v){
							$attr_num = $attr_num+$a_v_v['num'];
						}
						$data['productList'][$p_key]['num']=$attr_num;
					}
				}
				if($p_v['tag_type']){
					$data['productList'][$p_key]['tag_ico'] = "/Uploads/type_ico/tag_".$p_v['tag_type'].".jpg";
				}
			}
		}else{
			$data['productList'] = array();
		}

		echo $this->suc($data);
	}
}